package com.zmn.mcc.business.impl.role;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.dto.LoginStaff;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.mcc.business.interfaces.role.RolePermitBService;
import com.zmn.mcc.model.entity.role.McRolePermit;
import com.zmn.mcc.services.interfaces.role.RolePermitService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/14 23:04
 */
@Service
@Slf4j
public class RolePermitBServiceImpl implements RolePermitBService {
    protected Logger logger = LoggerFactory.getLogger(RolePermitBServiceImpl.class);

    @Resource
    private RolePermitService rolePermitService;


    // region 修改角色拥有的权限

    @Override
    public ResponseDTO modifyRolePermit(List<McRolePermit> rolePermits) {

        try {

            rolePermitService.insertByBatch(rolePermits);

            return ResponseDTO.success(null, "修改角色拥有的权限成功");

        } catch (Exception ex) {
            logger.error(String.format("[ZMN]修改角色拥有的权限失败,rolePermits:[%s],ex:[%s]", JSONObject.toJSONString(rolePermits), ex.getMessage()));
            return ResponseDTO.fail("修改角色拥有的权限失败");
        }

    }
    // endregion

    @Override
    public void addRolePermit(Integer permitId, List<Integer> roleIds, LoginStaff loginStaff, String ip) {
        try {

            List<McRolePermit> rolePermits = new ArrayList<>();
            for (Integer roleId : roleIds) {
                rolePermits.add(new McRolePermit(roleId, permitId));
            }
            rolePermitService.insertByBatch(rolePermits);
            log.info(String.format("[ZMN]为指定权限添加角色成功,permitId:[%s],roleIds:[%s],operator:[%s]", permitId, StringUtil.convertListToStrs(roleIds), loginStaff.getRealName()));
        } catch (Exception ex) {
            log.error(String.format("[ZMN]为指定权限添加角色出错,permitId:[%s],roleIds:[%s],operator:[%s],ex:[%s]", permitId, StringUtil.convertListToStrs(roleIds), loginStaff.getRealName(), ex.getMessage()));
        }
    }
}
